package org.nmgyjt.springboot.utils;

import com.alibaba.fastjson.JSONObject;
import com.nimbusds.jose.JOSEObjectType;
import com.nimbusds.jose.JWSAlgorithm;
import com.nimbusds.jose.JWSHeader;
import com.nimbusds.jose.JWSSigner;
import com.nimbusds.jose.crypto.RSASSASigner;
import com.nimbusds.jose.jwk.RSAKey;
import com.nimbusds.jwt.JWTClaimsSet;
import com.nimbusds.jwt.SignedJWT;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JWTUtil {
    public static String getToken(){
        JWSHeader jwsHeader = new JWSHeader.Builder(JWSAlgorithm.RS256)
                .type(JOSEObjectType.JWT)
                .keyID("25d10876-6be9-4392-b890-5199e6e5e1e0")
                .build();
        JWTClaimsSet claimsSet = new JWTClaimsSet.Builder()
                .issueTime(new Date(System.currentTimeMillis()))
                .expirationTime(new Date(System.currentTimeMillis() + 1000 * 60 * 10))
                .audience("audience")
                .issuer("6219e379-b3d1-4f75-b9fd-3ad3cb0bdda0")
                .claim("account_type", "serviceAccount")
                .build();
        SignedJWT jwt = new SignedJWT(jwsHeader, claimsSet);
        JSONObject privateMap=new JSONObject();
        privateMap.put("p","4IWDS2aT48OL2KI55NLXy3m4VWJdySetRT6tcKyEdEEw15gE-ZGidZgusnAKXVsXZ0iLNEzQiRh8SKIC2uojRaXbSqKDb4VxBQ_vZBGJSleCnZ4u0ocI7U10E-AtMwa5doZy1e7EJKsG6CP21jU3AAp1WT6HzLPcccQT6hb05dc");
        privateMap.put("kty","RSA");
        privateMap.put("q","qyCtWmqh5bwidC9IMdK_FtoU1u7d_1BA4C4uztdovW9Hmnm9v6ALsw5Hf2O3XOiySR5G2nqH1mrCwHD45KDfG9oT6qXmYP_z_9z9H-lXG-IjaBKOsrianrEkudlBcDgQB_1aSd9y0SGRW4R-6jRK7TkZgI0c0Ctx2K1hZrdSq58");
        privateMap.put("d","VUj6DsFKY1MOh5UDQLQxlJN-jSabBdoL8CkrHX9a1n2h6iNz5qM-alZ4ONNq6fF2a3dWgQJGAchS4mEDtpg5U9LAyXnO4k6nsOnK3RaTI3SZF-qH5SuCTVT7isxjlZhVc-ph8wZ4xtfbYULQhpQgvKSGDcMBv78hDYCxbfjYVu8VgGSBJWxuftpdaK_RUqim70KSLqEOJsItPpSt0jYAjegcftPYmMDHnO1PByKnwMscxQJh_3-L8JSu71TVXxyFXilPPRhQ2jb2MmW4KWaBD6s085JYIszamt-yUtcjg_M6Es8t_DB7V_TNDnrAitlHXEKwC_y9yMtDVVz5_IHJdQ");
        privateMap.put("e","AQAB");
        privateMap.put("use","sig");
        privateMap.put("kid","25d10876-6be9-4392-b890-5199e6e5e1e0");
        privateMap.put("qi","2fyd3eHswtVLg36_ao38wSUsrAXT0_ZvTUO3_gdG5ge6303n3n0sFDd8zwAn9M9BsN057LlnXk1aVqt9AzuiXqPgl-Q9yuMzxeKaDySI2A7MGFcv0hIZXFPjUS2lGPm16VswaoWmF3cq35S830VZzfbkJfF6xyVA-QJPOB5vyZQ");
        privateMap.put("dp","fxw3Y8W0LntzvfbCDgEsOL81k-l3rT1wREaizKpu5p5Nt5ESJ_TCZA7_SPhTddzvWALjBH4Ehck9-6T1NtjdlT278_ojrUx_2tzVq_Kdpl_6y59VZM0cSbTS5bnB_SRudWLSurlNhHDrHRvXUBiahcRmAVt8aHDfee0HVT0f8ss");
        privateMap.put("dq","IkQp0jgV8Q2qDH_HQgrELsw-bgdoto4YX4t_3fUuSPh2aw1DnJJspU8PrAeImPcTdCa241qzxpCcLLKyc_1uj4IsiMjth7DY5_wVR4WO5stycY19u3XRqEMqaniBbPoc46ysnqw6rnu9QokWbMlJ05SicaZQBbMdWdjl7w54ZFs");
        privateMap.put("n","lhXXbU0g_kVRX6aLrj-2JkTU6RGlqcOrywyYZ4ObmF5e_D_OWwWDcAi03h1n4nGaxxneALvno29fYjJKuCFhW9wX-oS-AIv4p8tXr44ORVu5uOevUq9bFNEeEqfsmd7KlS55pApkcLZ7iTrZG9oFxA1ozFyh8j52xpskYCvRZE1-eYdcpDdHOpL6JnlhBX9NkDqJcxjhrLgUwPcIVDY-d74lMgX54INUGYVE8n0mfpFWtWnml3TwLEyp3gtEmv2upyfzN97_acsr_mP4rSCwgiUkpmdUmex9vheM6Wczydsq8_fqFGqqDJSPKKCHkS1Ouq4tqGXX6T8EbzEiJ39diQ");
        try {
            RSAKey rsaKey = RSAKey.parse(privateMap.toJSONString());
            JWSSigner signer = new RSASSASigner(rsaKey, true);
            jwt.sign(signer);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return jwt.serialize();
    }

    public static void main(String[] args) {
        JWSHeader jwsHeader = new JWSHeader.Builder(JWSAlgorithm.RS256)
                .type(JOSEObjectType.JWT)
                .keyID("25d10876-6be9-4392-b890-5199e6e5e1e0")
                .build();
        JWTClaimsSet claimsSet = new JWTClaimsSet.Builder()
                .issueTime(new Date(System.currentTimeMillis()))
                .expirationTime(new Date(System.currentTimeMillis() + 1000 * 60 * 10))
                .audience("audience")
                .issuer("6219e379-b3d1-4f75-b9fd-3ad3cb0bdda0")
                .claim("account_type", "serviceAccount")
                .build();
        SignedJWT jwt = new SignedJWT(jwsHeader, claimsSet);
//        Map<String,Object> privateMap=new HashMap<>();
        JSONObject privateMap=new JSONObject();
        privateMap.put("p","4IWDS2aT48OL2KI55NLXy3m4VWJdySetRT6tcKyEdEEw15gE-ZGidZgusnAKXVsXZ0iLNEzQiRh8SKIC2uojRaXbSqKDb4VxBQ_vZBGJSleCnZ4u0ocI7U10E-AtMwa5doZy1e7EJKsG6CP21jU3AAp1WT6HzLPcccQT6hb05dc");
        privateMap.put("kty","RSA");
        privateMap.put("q","qyCtWmqh5bwidC9IMdK_FtoU1u7d_1BA4C4uztdovW9Hmnm9v6ALsw5Hf2O3XOiySR5G2nqH1mrCwHD45KDfG9oT6qXmYP_z_9z9H-lXG-IjaBKOsrianrEkudlBcDgQB_1aSd9y0SGRW4R-6jRK7TkZgI0c0Ctx2K1hZrdSq58");
        privateMap.put("d","VUj6DsFKY1MOh5UDQLQxlJN-jSabBdoL8CkrHX9a1n2h6iNz5qM-alZ4ONNq6fF2a3dWgQJGAchS4mEDtpg5U9LAyXnO4k6nsOnK3RaTI3SZF-qH5SuCTVT7isxjlZhVc-ph8wZ4xtfbYULQhpQgvKSGDcMBv78hDYCxbfjYVu8VgGSBJWxuftpdaK_RUqim70KSLqEOJsItPpSt0jYAjegcftPYmMDHnO1PByKnwMscxQJh_3-L8JSu71TVXxyFXilPPRhQ2jb2MmW4KWaBD6s085JYIszamt-yUtcjg_M6Es8t_DB7V_TNDnrAitlHXEKwC_y9yMtDVVz5_IHJdQ");
        privateMap.put("e","AQAB");
        privateMap.put("use","sig");
        privateMap.put("kid","25d10876-6be9-4392-b890-5199e6e5e1e0");
        privateMap.put("qi","2fyd3eHswtVLg36_ao38wSUsrAXT0_ZvTUO3_gdG5ge6303n3n0sFDd8zwAn9M9BsN057LlnXk1aVqt9AzuiXqPgl-Q9yuMzxeKaDySI2A7MGFcv0hIZXFPjUS2lGPm16VswaoWmF3cq35S830VZzfbkJfF6xyVA-QJPOB5vyZQ");
        privateMap.put("dp","fxw3Y8W0LntzvfbCDgEsOL81k-l3rT1wREaizKpu5p5Nt5ESJ_TCZA7_SPhTddzvWALjBH4Ehck9-6T1NtjdlT278_ojrUx_2tzVq_Kdpl_6y59VZM0cSbTS5bnB_SRudWLSurlNhHDrHRvXUBiahcRmAVt8aHDfee0HVT0f8ss");
        privateMap.put("dq","IkQp0jgV8Q2qDH_HQgrELsw-bgdoto4YX4t_3fUuSPh2aw1DnJJspU8PrAeImPcTdCa241qzxpCcLLKyc_1uj4IsiMjth7DY5_wVR4WO5stycY19u3XRqEMqaniBbPoc46ysnqw6rnu9QokWbMlJ05SicaZQBbMdWdjl7w54ZFs");
        privateMap.put("n","lhXXbU0g_kVRX6aLrj-2JkTU6RGlqcOrywyYZ4ObmF5e_D_OWwWDcAi03h1n4nGaxxneALvno29fYjJKuCFhW9wX-oS-AIv4p8tXr44ORVu5uOevUq9bFNEeEqfsmd7KlS55pApkcLZ7iTrZG9oFxA1ozFyh8j52xpskYCvRZE1-eYdcpDdHOpL6JnlhBX9NkDqJcxjhrLgUwPcIVDY-d74lMgX54INUGYVE8n0mfpFWtWnml3TwLEyp3gtEmv2upyfzN97_acsr_mP4rSCwgiUkpmdUmex9vheM6Wczydsq8_fqFGqqDJSPKKCHkS1Ouq4tqGXX6T8EbzEiJ39diQ");
        try {
            RSAKey rsaKey = RSAKey.parse(privateMap.toJSONString());
            JWSSigner signer = new RSASSASigner(rsaKey, true);
            jwt.sign(signer);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        String token = jwt.serialize();
        System.out.println(token);
    }
}
